package com.gongpingjialoan.adapter.car;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.car.BaseBean;
import com.gongpingjialoan.bean.car.MainTainData;
import com.gongpingjialoan.bean.car.PriceAdjustmentBean;
import com.gongpingjialoan.network.NetDataJson;

import java.util.List;


/**
 * Created by Administrator on 2016/1/26
 */
public class PriceAdjustmentRecordAdapter extends BaseAdapter {
    LayoutInflater inflater;
    Context context;

    private List<PriceAdjustmentBean> list;

    private NetDataJson maintainNetDataJson;
    private BaseBean<MainTainData> mainTainData;

    public PriceAdjustmentRecordAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        if (list != null) {
            return list.size();
        }
        return 0;
    }

    @Override
    public PriceAdjustmentBean getItem(int position) {
        if (list != null) {
            return list.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setData(List<PriceAdjustmentBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_price_adjustment_record_list,null);

            holder.dateT = (TextView) convertView.findViewById(R.id.date);
            holder.labelT = (TextView) convertView.findViewById(R.id.label);
            holder.priceT = (TextView) convertView.findViewById(R.id.price);
            holder.price_changeT = (TextView) convertView.findViewById(R.id.price_change);
            holder.has_value = (RelativeLayout) convertView.findViewById(R.id.has_value);
            holder.has_not_value = convertView.findViewById(R.id.has_not_value);
            holder.price_change_main = convertView.findViewById(R.id.price_change_main);
            holder.mImageView = (ImageView) convertView.findViewById(R.id.change_icon);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        PriceAdjustmentBean bean = getItem(position);

        holder.dateT.setText(bean.getCreate_time());
        holder.priceT.setText(bean.getPrice()+"万");
        float price_change = Float.valueOf(bean.getPrice_change());
        if (price_change > 0){
            holder.dateT.setTextColor(context.getResources().getColor(R.color.ed8));
            holder.dateT.setBackgroundResource(R.drawable.down_price_bg);
            holder.labelT.setTextColor(context.getResources().getColor(R.color.ed8));
            holder.priceT.setTextColor(context.getResources().getColor(R.color.ed8));
            holder.price_change_main.setBackgroundResource(R.drawable.down_price_bg);
            holder.mImageView.setImageResource(R.drawable.down_icon);
            holder.price_changeT.setText(price_change + "万元");
            holder.price_changeT.setTextColor(context.getResources().getColor(R.color.ed8));
        }else {
            holder.dateT.setTextColor(context.getResources().getColor(R.color.ed5));
            holder.dateT.setBackgroundResource(R.drawable.up_price_bg);
            holder.labelT.setTextColor(context.getResources().getColor(R.color.ed5));
            holder.priceT.setTextColor(context.getResources().getColor(R.color.ed5));

            holder.mImageView.setImageResource(R.drawable.up_icon);
            holder.price_change_main.setBackgroundResource(R.drawable.up_price_bg);
            holder.price_changeT.setText(Math.abs(price_change) + "万元");
            holder.price_changeT.setTextColor(context.getResources().getColor(R.color.ed5));
        }
        if (position == 0){
            holder.dateT.setTextColor(context.getResources().getColor(R.color.new_color));
            holder.dateT.setBackgroundResource(R.drawable.frist_price_bg);
            holder.labelT.setTextColor(context.getResources().getColor(R.color.new_color));
            holder.priceT.setTextColor(context.getResources().getColor(R.color.new_color));
            holder.has_value.setVisibility(View.VISIBLE);
            holder.has_not_value.setVisibility(View.GONE);
            holder.labelT.setText("首次标价");
            holder.price_change_main.setVisibility(View.INVISIBLE);
        }else if (bean.is_lowest()){
            holder.has_value.setVisibility(View.VISIBLE);
            holder.has_not_value.setVisibility(View.GONE);
            holder.price_change_main.setVisibility(View.VISIBLE);
            holder.labelT.setText("最低标价");
        //    holder.priceT.setTextColor(context.getResources().getColor(R.color.text_green_result));
         //   holder.labelT.setTextColor(context.getResources().getColor(R.color.text_green_result));
        }else {
            holder.has_value.setVisibility(View.GONE);
            holder.has_not_value.setVisibility(View.VISIBLE);
            holder.price_change_main.setVisibility(View.VISIBLE);
         //   holder.priceT.setTextColor(context.getResources().getColor(R.color.text_orange));
        }


        return convertView;
    }

    class ViewHolder {
        ImageView mImageView;
        TextView dateT;
        TextView labelT;
        TextView priceT;
        TextView price_changeT;
        RelativeLayout has_value;
        View has_not_value , price_change_main;
    }

    private SpannableStringBuilder changeColor(String front,String change,String behind){
        ForegroundColorSpan mileForegroundColorSpan = new ForegroundColorSpan(context.getResources().getColor(R.color.text_orange));
        String title = front + change + behind;
        SpannableStringBuilder mileBuilder = new SpannableStringBuilder(title);
        mileBuilder.setSpan(mileForegroundColorSpan, front.length(),front.length()+change.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return mileBuilder;
    }

}
